package haizhi.fruitmall.service.impl;

import haizhi.fruitmall.constant.OrderMsg;
import haizhi.fruitmall.dao.mapper.OrderGoodsMapper;
import haizhi.fruitmall.dao.mapper.OrderRecevingMapper;
import haizhi.fruitmall.dao.mapper.OrdersMapper;
import haizhi.fruitmall.dao.mapper.RecevingInfoDefaultMapper;
import haizhi.fruitmall.dao.mapper.RecevingInfoMapper;
import haizhi.fruitmall.dao.mapper.UserRecevingMapper;
import haizhi.fruitmall.pojo.po.OrderGoods;
import haizhi.fruitmall.pojo.po.OrderGoodsExample;
import haizhi.fruitmall.pojo.po.OrderReceving;
import haizhi.fruitmall.pojo.po.Orders;
import haizhi.fruitmall.pojo.po.OrdersExample;
import haizhi.fruitmall.pojo.po.RecevingInfo;
import haizhi.fruitmall.pojo.po.RecevingInfoDefault;
import haizhi.fruitmall.pojo.po.RecevingInfoDefaultExample;
import haizhi.fruitmall.pojo.po.UserReceving;
import haizhi.fruitmall.pojo.po.UserRecevingExample;
import haizhi.fruitmall.service.OrderService;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrdersMapper ordersMapper;

	@Autowired
	private OrderGoodsMapper orderGoodsMapper;

	@Autowired
	private RecevingInfoMapper recevingInfoMapper;

	@Autowired
	private OrderRecevingMapper orderRecevingMapper;

	@Autowired
	private RecevingInfoDefaultMapper recevingInfoDefaultMapper;

	@Autowired
	private UserRecevingMapper userRecevingMapper;

	@Override
	public void addOrder(Orders order) {
		ordersMapper.insertSelective(order);
	}

	@Override
	public void addOrderGoods(OrderGoods orderGoods) {
		orderGoodsMapper.insertSelective(orderGoods);
	}

	@Override
	public void addRecevingInfo(RecevingInfo recevingInfo) {
		recevingInfoMapper.insertSelective(recevingInfo);
	}

	@Override
	public void addOrderReceving(OrderReceving orderReceving) {
		orderRecevingMapper.insertSelective(orderReceving);
	}

	@Override
	public void addRecevingInfoDefault(RecevingInfoDefault recevingInfoDefault) {
		recevingInfoDefaultMapper.insertSelective(recevingInfoDefault);
	}

	@Override
	public void addUserRecevingInfo(UserReceving userReceving) {
		userRecevingMapper.insertSelective(userReceving);
	}

	@Override
	public void deleteRecevingInfoDefault(String receving_id) {
		RecevingInfoDefaultExample recevingInfoDefaultExample = new RecevingInfoDefaultExample();
		RecevingInfoDefaultExample.Criteria criteria = recevingInfoDefaultExample
				.createCriteria();
		criteria.andRecevingIdEqualTo(receving_id);
		recevingInfoDefaultMapper.deleteByExample(recevingInfoDefaultExample);
	}

	@Override
	public void deleteRecevingInfo(String receving_id) {
		recevingInfoMapper.deleteByPrimaryKey(receving_id);
	}

	@Override
	public void deleteUserReceving(String receving_id) {
		UserRecevingExample userRecevingExample = new UserRecevingExample();
		UserRecevingExample.Criteria criteria = userRecevingExample
				.createCriteria();
		criteria.andRecevingIdEqualTo(receving_id);
		userRecevingMapper.deleteByExample(userRecevingExample);
	}

	@Override
	public void deleteOrderById(String order_id) {
		ordersMapper.deleteByPrimaryKey(order_id);
	}

	@Override
	public void deleteOrder(Orders order) {
		OrdersExample orderExample = new OrdersExample();
		OrdersExample.Criteria criteria = orderExample.createCriteria();
		if (order.getUserId() != null) {
			criteria.andUserIdEqualTo(order.getUserId());
		}
		if (order.getOrderingTime() != null) {
			criteria.andOrderingTimeLessThanOrEqualTo(order.getOrderingTime());
		}
		if (order.getOrderId() != null) {
			criteria.andOrderIdEqualTo(order.getOrderId());
		}
		if (order.getOrderId() != null || order.getUserId() != null
				|| order.getOrderId() != null) {
			ordersMapper.deleteByExample(orderExample);
		}
	}

	@Override
	public void deleteOrderGoods(OrderGoods orderGoods) {
		OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
		OrderGoodsExample.Criteria criteria = orderGoodsExample
				.createCriteria();
		if (orderGoods.getOrderId() != null) {
			criteria.andOrderIdEqualTo(orderGoods.getOrderId());
		}
		orderGoodsMapper.deleteByExample(orderGoodsExample);
	}

	@Override
	public void deleteOrderReceving(String order_id) {

		orderRecevingMapper.deleteByPrimaryKey(order_id);

	}

	@Override
	public void updateOrder(Orders order, Map map) {
		OrdersExample orderExample = new OrdersExample();
		OrdersExample.Criteria criteria = orderExample.createCriteria();
		String order_status = (String) map.get(OrderMsg.ORDER_STATUS);
		String shipment_material = (String) map.get(OrderMsg.SHIPMENT_MATERIAL);
		String shopper_type = (String) map.get(OrderMsg.SHOPPER_TYPE);
		String order_freight = (String) map.get(OrderMsg.ORDER_FREIGHT);
		String order_id = (String) map.get(OrderMsg.ORDER_ID);

		if (order_status != null) {
			criteria.andOrderStatusEqualTo(new Byte(order_status));
		}
		if (shipment_material != null) {
			criteria.andShipmentMaterialEqualTo(shipment_material);
		}
		if (shopper_type != null) {
			criteria.andShopperTypeEqualTo(shopper_type);
		}
		if (order_freight != null) {
			criteria.andOrderFreightEqualTo(Float.parseFloat(order_freight));
		}
		if (order_id != null) {
			criteria.andOrderIdEqualTo(order_id);
		}
		ordersMapper.updateByExampleSelective(order, orderExample);
	}

	@Override
	public void updateRecevingInfo(RecevingInfo recevingInfo) {
		recevingInfoMapper.updateByPrimaryKeySelective(recevingInfo);
	}

	@Override
	public void updateRecevingDefault(RecevingInfoDefault recevingInfoDefault) {
		RecevingInfoDefaultExample recevingInfoDefaultExample = new RecevingInfoDefaultExample();
		RecevingInfoDefaultExample.Criteria criteria = recevingInfoDefaultExample
				.createCriteria();
		criteria.andUserIdEqualTo(recevingInfoDefault.getUserId());
		recevingInfoDefaultMapper.updateByExampleSelective(recevingInfoDefault,
				recevingInfoDefaultExample);
	}

	@Override
	public PageInfo getPageInfoOfAllOrders(int currentPage, int pageSize) {
		PageHelper.startPage(currentPage, pageSize);
		OrdersExample orderExample = new OrdersExample();
		OrdersExample.Criteria criteria = orderExample.createCriteria();

		// 排序条件
		orderExample.setOrderByClause("ordering_time desc");
		List<Orders> orders = ordersMapper.selectByExample(orderExample);
		return new PageInfo(orders);
	}

	@Override
	public Orders getOrderByOrderId(String order_id) {
		return ordersMapper.selectByPrimaryKey(order_id);
	}

	@Override
	public List<OrderGoods> getOrderGoodsByOrderId(String order_id) {
		OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
		OrderGoodsExample.Criteria criteria = orderGoodsExample
				.createCriteria();
		criteria.andOrderIdEqualTo(order_id);
		return orderGoodsMapper.selectByExample(orderGoodsExample);
	}

	@Override
	public OrderReceving getOrderReceving(String order_id) {
		return orderRecevingMapper.selectByPrimaryKey(order_id);
	}

	@Override
	public PageInfo getPageInfoOfOrderByUserName(Integer user_id,
			Byte order_status, int currentPage, int pageSize) {
		PageHelper.startPage(currentPage, pageSize);
		OrdersExample orderExample = new OrdersExample();
		OrdersExample.Criteria criteria = orderExample.createCriteria();
		criteria.andUserIdEqualTo(user_id);
		if (order_status != null) {
			criteria.andOrderStatusEqualTo(order_status);
		}
		orderExample.setOrderByClause("ordering_time desc");
		List<Orders> orders = ordersMapper.selectByExample(orderExample);
		return new PageInfo(orders);
	}

	@Override
	public RecevingInfo getRecevingInfoByRecevingId(String receving_id) {
		return recevingInfoMapper.selectByPrimaryKey(receving_id);
	}

	@Override
	public List<RecevingInfoDefault> getRecevingInfoDefaultByUserId(
			Integer user_id) {
		RecevingInfoDefaultExample recevingInfoDefaultExample = new RecevingInfoDefaultExample();
		RecevingInfoDefaultExample.Criteria criteria = recevingInfoDefaultExample
				.createCriteria();
		criteria.andUserIdEqualTo(user_id);
		return recevingInfoDefaultMapper
				.selectByExample(recevingInfoDefaultExample);
	}

	@Override
	public List<UserReceving> getUserRecevingByUserId(Integer user_id) {
		UserRecevingExample userRecevingExample = new UserRecevingExample();
		UserRecevingExample.Criteria criteria = userRecevingExample
				.createCriteria();
		criteria.andUserIdEqualTo(user_id);
		return userRecevingMapper.selectByExample(userRecevingExample);
	}
}
